var app = getApp()
var gb = app.globalData
var Bmob = gb.Bmob
var SessionMgr = gb.SessionMgr
var BmobObj = gb.BmobObj

var Define = require("../../../lib/Define.js")

var _sessionUser = null

Page({
  data:{
    orders:null
  },
  onLoad:function(options){
    
    var self = this

    Define.getUserInfo(function(userInfo){
      _sessionUser = userInfo

      if(userInfo){
        wx.request({
          url: Define.value.baseUrl+'/app/order/list?userId='+userInfo.id,
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            // success
            var data = res.data
            if(data.code == 200){
              var list = []
              for(var index = 0 ; index < data.data.length; index++){
                var item = data.data[index]
                item.mainImage = item.ware.images[0].url
                item.uDateStr = Define.dateFrom(item.uDate,"yyyy-MM-dd")
                list.push(item)

                console.log(item)
              }

              self.setData({
                orders:list
              })
            }
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
      }
    })

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  reqOrderList:function(){
    //请求我的订单列表
    console.log(_sessionUser)
    var self = this

    var Order = Bmob.Object.extend("order")
    var query = new Bmob.Query(Order)
    query.equalTo("consumer_id",_sessionUser.objectId)
    query.find({
      success:function(results){
        var orders = BmobObj.func.orderList(results)
        console.log(orders)
        self.setData({
          orders:orders
        })
      },
      error:function(err){
        console.log(err)
      }
    })
  }
})